<?php

class CliController extends CI_Controller
{
	public function index()
	{
		$offset = 0;
		$sql = "select * from db_product limit 1000 offset $offset";
		$result = $this->db->query($sql);
		$this->db->trans_start();
		while ($result->num_rows() > 0) {
			while ($row = $result->unbuffered_row('array')) {
				$product = [];
				$product['id'] = $row['P_ID'];
				$product['category_id'] = intval($row['P_FType']);
				$product['category_id2'] = intval($row['P_STYPE']);
				$product['brand_id'] = $row['P_Brand'];
				$product['title'] = strval($row['P_Title']);
				$product['en_title'] = strval($row['P_EnTitle']);
				$product['hits'] = $row['P_Hits'];
				$product['solded'] = $row['P_Sold'];
				$product['stock'] = $row['P_KC'];
				$product['status'] = $row['P_ShowFlag'];
				$product['price'] = $row['P_Price'];
				$product['original_price'] = $row['P_OldPrice'];
				$product['images'] = $row['P_Images'];
				$product['content'] = $row['P_Content'];
				$product['en_content'] = $row['P_EnContent'];
				$product['thumbnail'] = $row['P_Pic'];
				$product['is_hot'] = 0;
				$product['sort'] = $row['P_Sort'];
				$product['is_delete'] = $row['P_ShowFlag'];
				$product['create_time'] = $row['P_AddTime'];
				$product['update_time'] = $row['P_UpdateTime'];
				$product['unit'] = '';
				$product['supplier_id'] = intval($row['P_SupplierId']);
				$product['barcode'] = '';
				$product['is_new'] = 0;
				$product['promotion'] = 0;
				$product['is_recommend'] = 0;
				$product['recommends'] = '';

				//保存图片
				if ($row['P_Pic']) {
					if (!file_exists($row['P_Pic'])) {
						$thumbnail = file_get_contents('http://www.rcbao-online.com/'.$row['P_Pic']);
						$pathinfo = pathinfo(APPPATH.$row['P_Pic']);
						if(!is_dir($pathinfo['dirname'])) {
							mkdir($pathinfo['dirname'], 0777, true);
						}
						$fp = fopen(APPPATH.$row['P_Pic'], 'w+');
						if (!$fp) {
							die("save image failed:".$row['P_Pic']);
						}
						fwrite($fp, $thumbnail);
						fclose($fp);
					}
				}
				
				$images = [];
				$image_files = array_filter(explode(',', $row['P_Images']));
				foreach ($image_files as $key => $value) {
					if (!file_exists($value)) {
						$content = file_get_contents('http://www.rcbao-online.com/'.$value);
						$pathinfo = pathinfo(APPPATH.$value);
						if(!is_dir($pathinfo['dirname'])) {
							mkdir($pathinfo['dirname'], 0777, true);
						}
						$fp = fopen(APPPATH.$value, 'w+');
						if (!$fp) {
								die("save image failed:".$value);
							}
						fwrite($fp, $content);
						fclose($fp);
					}
				}

				$this->db->insert('products', $product);
				echo '.';
			}
			$offset += 1000;
			$sql = "select * from db_product limit 1000 offset $offset";
			$result = $this->db->query($sql);
			echo "\nnow offset:$offset\n";
		}
		$this->db->trans_complete();
		echo "\n complete \n";
	}

	public function brand()
	{
		$offset = 0;
		$sql = "select * from db_brand limit 1000 offset $offset";
		$result = $this->db->query($sql);
		$this->db->trans_start();
		while ($result->num_rows() > 0) {
			while ($row = $result->unbuffered_row('array')) {
				$brand = [];
				$brand['id'] = $row['B_ID'];
				$brand['title'] = $row['B_Title'];
				$brand['en_title'] = $row['B_EnTitle'];
				$brand['image'] = $row['B_Pic'];
				$brand['sort'] = $row['B_Sort'];
				$brand['categories'] = '';
				$this->db->insert('brands', $brand);
				echo '.';
			}
			$offset += 1000;
			$sql = "select * from db_brand limit 1000 offset $offset";
			$result = $this->db->query($sql);
			echo "\nnow offset:$offset\n";
		}
		$this->db->trans_complete();
		echo "\n complete \n";
	}

	public function category()
	{
		$result = $this->db->query('select * from db_projecttype');
		$this->db->trans_start();
		while ($row = $result->unbuffered_row('array')) {
			$category = [];

			$category['id'] = $row['PT_ID'];
			$category['title'] = $row['PT_Title'];
			$category['en_title'] = $row['PT_EnTitle'];
			$category['image'] = '';
			$category['parent_id'] = $row['PT_F'];
			$category['sort'] = $row['PT_Sort'];
			$category['ad'] = '';

			$this->db->insert('categories', $category);
		}
		$this->db->trans_complete();
		echo "\nsuccess\n";
	}

	public function repair()
	{
		$result = $this->db->query('select * from products where images like \'images%\'');
		$this->db->trans_start();
		while ($row = $result->unbuffered_row('array')) {
			$this->db->where('id', $row['id'])->update('products', [
				'images' => '/'.$row['images'],
			]);
			echo '.';
		}
		$this->db->trans_complete();
		echo 'complete';
	}

	public function items()
	{
		$result = $this->db->query('select * from products left join items on items.product_id=products.id where items.id is null');
		$this->db->trans_start();
		while ($row = $result->unbuffered_row('array')) {
			$this->db->query("INSERT INTO `items` (`product_id`, `title`, `en_title`, `price`, `original_price`, `tax`, `barcode`, `stock`, `danger_stock`, `solded`, `weight`, `is_delete`)
VALUES
	({$row['id']}, '1', '1', '1', 1.00, 1.00, '1', 0, 5, 0, 1, 0);");
			echo '.';
		}
		$this->db->trans_complete();
	}
}